/*
 * Created by JFormDesigner on Fri Jan 08 09:52:12 CST 2021
 */

package com.xxl.javawork;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.event.*;

/**
 *三角形计算器的JPanel类，实现三角形计算的GUI
 * @author xiaxinlin
 * @version 1.0
 * @since jdk1.8
 * @date 2021/1/9 22:40
 */
public class TriCalaulator extends JPanel {

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JButton button1;
    private JLabel label1;
    //边长a
    private JTextField textField1;
    private JLabel label2;
    private JLabel label3;
    //滚动条
    private JScrollPane scrollPane1;
    //历史记录jlist
    private JList list1;
    private JLabel label4;
    //边长b
    private JTextField textField2;
    private JLabel label5;
    //边长c
    private JTextField textField3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables

    private List<TriangleCal> list;
    //历史记录的listmode
    private DefaultListModel listModel;
    private TriangleCal triangleCal;


    public TriCalaulator() {
        initComponents();
    }

    /**
     * 计算按钮监听事件
     *
     * @param e
     * @return void
     * @author xiaxinlin
     * @creed: Talk is cheap,show me the code
     * @date 2021/1/9
     */
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        String str1 = textField1.getText();
        String str2 = textField2.getText();
        String str3 = textField3.getText();
        if (e.getSource()==button1&&!"".equals(str1.trim())&&!"".equals(str2.trim())&&!"".equals(str3.trim())){
            double a = Double.parseDouble(str1);
            double b = Double.parseDouble(str2);
            double c = Double.parseDouble(str3);
            triangleCal = new TriangleCal(a,b,c);
            listModel.addElement("周长："+triangleCal.getGirth()+"   面积："+triangleCal.getArea());
            list.add(0,triangleCal);

            list1.setSelectedIndex(0);
        }
    }

    /**
     * //历史记录的JList监听事件方法
     *
     * @param e
     * @return void
     * @author xiaxinlin
     * @creed: Talk is cheap,show me the code
     * @date 2021/1/9
     */
    private void list1ValueChanged(ListSelectionEvent e) {
        // TODO add your code here
        int index = list1.getSelectedIndex();
        TriangleCal triangleCal1 = list.get(index);
        textField1.setText(triangleCal1.getSideA()+"");
        textField2.setText(triangleCal1.getSideB()+"");
        textField3.setText(triangleCal1.getSideC()+"");
    }

    /**
     * 初始化JPanel方法
     *
     * @return void
     * @author xiaxinlin
     * @creed: Talk is cheap,show me the code
     * @date 2021/1/9
     */
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        button1 = new JButton();
        label1 = new JLabel();
        textField1 = new JTextField();
        label2 = new JLabel();
        label3 = new JLabel();
        scrollPane1 = new JScrollPane();
        list1 = new JList();
        label4 = new JLabel();
        textField2 = new JTextField();
        label5 = new JLabel();
        textField3 = new JTextField();

        //======== this ========
        setLayout(null);

        //---- button1 ----
        button1.setText("\u8ba1\u7b97");
        button1.addActionListener(e -> {
			button1ActionPerformed(e);
		});
        add(button1);
        button1.setBounds(new Rectangle(new Point(155, 340), button1.getPreferredSize()));

        //---- label1 ----
        label1.setText("\u8fb9\u957f\uff1a");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 7f));
        add(label1);
        label1.setBounds(40, 110, 65, 35);
        add(textField1);
        textField1.setBounds(135, 120, 100, textField1.getPreferredSize().height);

        //---- label2 ----
        label2.setText("\u8ba1\u7b97\u4e09\u89d2\u5f62\u7684\u5468\u957f\u548c\u9762\u79ef");
        label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 6f));
        add(label2);
        label2.setBounds(45, 40, 210, 30);

        //---- label3 ----
        label3.setText("\u5386\u53f2\u8bb0\u5f55");
        add(label3);
        label3.setBounds(355, 60, 130, 20);

        //======== scrollPane1 ========
        {

            //---- list1 ----
            list1.setFont(list1.getFont().deriveFont(list1.getFont().getSize() + 3f));
            list1.addListSelectionListener(e -> list1ValueChanged(e));
            scrollPane1.setViewportView(list1);
        }
        add(scrollPane1);
        scrollPane1.setBounds(355, 85, 185, 285);

        //---- label4 ----
        label4.setText("\u8fb9\u957f\uff1a");
        label4.setFont(label4.getFont().deriveFont(label4.getFont().getSize() + 7f));
        add(label4);
        label4.setBounds(40, 170, 65, 35);
        add(textField2);
        textField2.setBounds(135, 175, 100, 24);

        //---- label5 ----
        label5.setText("\u8fb9\u957f\uff1a");
        label5.setFont(label5.getFont().deriveFont(label5.getFont().getSize() + 7f));
        add(label5);
        label5.setBounds(40, 225, 65, 35);
        add(textField3);
        textField3.setBounds(135, 230, 100, 24);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < getComponentCount(); i++) {
                Rectangle bounds = getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            setMinimumSize(preferredSize);
            setPreferredSize(preferredSize);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents

        this.setBounds(0, 0, 567, 480);
        list = new ArrayList();
        listModel = new DefaultListModel();
        list1.setModel(listModel);
    }

}
